package com.yuanchu.order.controller;

import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.order.dto.*;
import com.yuanchu.order.entity.YoshopOrderEntity;
import com.yuanchu.order.service.YoshopOrderService;
import com.yuanchu.order.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 订单接口
 * @date: 2024-01-24 9:53
 */
@Api(value = "OrderController", tags = "订单接口")
@RestController
public class OrderController {

    @Resource
    private YoshopOrderService orderService;
    Integer storeId = 1001;

    // /api/order/list&dataType=all&page=1
    @ApiOperation(value = "/order/list&dataType=all&page=1", tags = "查询用户订单列表")
    @GetMapping("/list")
    public R getOrderList(@RequestParam String dataType, @RequestParam Integer page) {
        // 根据dataType和page获取用戶订单列表数据
        Integer userId = SecurityUtil.getUserId();
        PageResult<OrderDetailDto> orderList = orderService.queryOrderList(dataType, page, userId);
        Map<String, Object> map = new HashMap<>();
        map.put("list", orderList);
        return R.ok().put("data", map);
    }

    @ApiOperation(value = "/order/detail&orderId=3", tags = "根据订单id 查询订单详情")
    @GetMapping("/detail2")
    public R getOrderDetail2(@RequestParam Integer orderId) {
        Integer userId = SecurityUtil.getUserId();
        OrderDetailDto order = orderService.getOrderDetailByOrderId(orderId, userId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("order", order);
        Map<String, Object> setting = new HashMap<>();
        setting.put("points_name", "积分");
        resultMap.put("setting", setting);
        return R.ok().put("data", resultMap);
    }


    @ApiOperation(value = "/order/detail&orderId=3", tags = "根据订单id 查询订单详情")
    @GetMapping("/detail")
    public OrderDetailDto getOrderDetail(@RequestParam Integer orderId) {
        Integer userId = SecurityUtil.getUserId();
        return orderService.getOrderDetailByOrderId(orderId, userId);
    }


    @GetMapping("/sales/month")
    public List<YoshopOrderEntity> queryOrderSalesByMonth() {
        return orderService.queryOrderSalesByMonth();
    }

    @GetMapping("/sales/overview")
    public Overview queryOverview() {
        return orderService.queryOverview();
    }

    @GetMapping("/statistic")
    public Map<String, String> queryOrderStatistics() {
        return orderService.queryOrderStatistics();
    }


    @ApiOperation(value = "/order/count/pending", tags = "统计订单数")
    @GetMapping("/count/pending")
    public Map<String, String> queryPending() {
        return orderService.queryPending();
    }

    @ApiOperation(value = "/order/trend", tags = "查询订单趋势")
    @GetMapping("/count/trend")
    public TradeTrendVO getTradeTrend() {
        return orderService.getTradeTrend();
    }


    @ApiOperation(value = "/order/todoCounts", tags = "查询统计处理订单数")
    @GetMapping("/todoCounts")
    public R queryOrderCounts() {
        Integer userId = SecurityUtil.getUserId();
        UserOrderCountsDto userOrderCounts = orderService.queryOrderCounts(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("counts", userOrderCounts);
        return R.ok().put("data", map);
    }

    @ApiOperation(value = "/order/pay", tags = "支付订单")
    @GetMapping("/pay")
    public R payOrder(@RequestParam("orderId") Integer orderId, @RequestParam("payType") Integer payType) {
        Integer userId = SecurityUtil.getUserId();
        boolean pay = orderService.payOrderStatus(userId, orderId, payType);
        if (!pay) {
            return R.error("支付失败");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("order_id", orderId);
        map.put("pay_type", payType);
        map.put("payment", Collections.EMPTY_LIST);
        return R.ok().put("data", map);
    }

    @ApiOperation(value = "/order/cancel", tags = "取消订单")
    @PostMapping("/cancel")
    public R cancelOrder(@RequestBody @Validated(ValidationGroups.Update.class) EditOrderDto order) {
        Integer userId = SecurityUtil.getUserId();
        boolean cancel = orderService.cancelOrder(userId, order.getOrderId());
        if (!cancel) {
            return R.error("取消失败");
        }
        return R.ok("申请取消成功");
    }

    @ApiOperation(value = "/order/receipt", tags = "确认收货")
    @PostMapping("/receipt")
    public R receiptOrder(@RequestBody @Validated(ValidationGroups.Update.class) EditOrderDto order) {
        Integer userId = SecurityUtil.getUserId();
        Integer orderId = order.getOrderId();
        boolean cancel = orderService.receiptOrder(userId, orderId);
        if (!cancel) {
            return R.error("确认失败");
        }
        return R.ok("确认成功");
    }


}
